package com.JZ.QM.controller;

import com.JZ.QM.common.entity.CombinationDynamicInfoEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.feign.CombinationFeignService;
import com.JZ.QM.service.CombinationDynamicInfoService;
import com.JZ.QM.service.ProYieldService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @author lyp
 * @email lyp@gmail.com
 * @date 2020/6/15 14:24
 */
@RefreshScope
@RestController
@Slf4j
@RequestMapping("/combinationDynamicInfo")
public class CombinationDynamicInfoController {
    @Resource
    CombinationDynamicInfoService combinationDynamicInfoService;
    @Resource
    private CombinationFeignService combinationFeignService;
    @Resource
    ProYieldService proYieldService;


    /**
     * 获取组合的详细信息
     * @param comBaseId：组合的基本信息
     * @return
     */
    @RequestMapping("/combinationDetailData")
    public CommonResult getCombinationDetailData(Long comBaseId){
        //组合的动态信息(收藏人数，访问人数，购买人数，交易人数)
        CombinationDynamicInfoEntity combinationDynamicInfoEntity;
        //组合的实体
        LinkedHashMap combinationBaseInfoEntityMap;
        //组合信息返回结果
         CommonResult commonResult=CommonResult.success();

        //获取组合的基本信息
        combinationBaseInfoEntityMap= (LinkedHashMap) combinationFeignService.getComBaseInfo(comBaseId).get("combination");
        if (combinationBaseInfoEntityMap == null ){
            return CommonResult.error(201,"该组合产品不存在");
        } else {
            //获取组合基本信息详细数据
            commonResult.put("comBaseName",combinationBaseInfoEntityMap.get("comBaseName"));
            commonResult.put("comBaseDesc",combinationBaseInfoEntityMap.get("comBaseDesc"));
            //获取组合的动态信息
            combinationDynamicInfoEntity= combinationDynamicInfoService.getDynasticInfoByCombinationId(Long.valueOf( combinationBaseInfoEntityMap.get("comBaseId").toString()));
            //获取组合动态信息详细数据
            commonResult.put("collectNum",combinationDynamicInfoEntity.getCollectNum());
            commonResult.put("visitNum",combinationDynamicInfoEntity.getVisitNum());
            commonResult.put("transactionNum",combinationDynamicInfoEntity.getTransactionNum());
            commonResult.put("purchaseNum",combinationDynamicInfoEntity.getPurchaseNum());
            return commonResult;
        }
    }

    /**
     * 更新组合的动态信息
     * @return
     */
    @RequestMapping("/updateCombinationDynamicInfo")
    public CommonResult updateCombinationDynamicInfo(@RequestParam("comBaseId")Long comBaseId,
                                                     @RequestParam("collectNum")Integer collectNum,
                                                     @RequestParam("visitNum") Integer visitNum,
                                                     @RequestParam("transactionNum") Integer transactionNum,
                                                     @RequestParam("purchaseNum")Integer purchaseNum){
        //用户收藏更新状态
        boolean comDynamicState=false;
        System.out.println("已经进入");
        //组合动态信息更新状态
        comDynamicState=combinationDynamicInfoService.updateComDynamic(comBaseId,collectNum,visitNum,transactionNum,purchaseNum);

        if(comDynamicState) {
            return CommonResult.success().put("comDynamicState",comDynamicState);
        } else {
            return CommonResult.error(201,"用户状态更新失败").put("comDynamicState",comDynamicState);
        }
    }
}
